/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.ModeloEntrega;
import com.ipartek.formacion.gestformwww.modelo.ModeloMaterial;

import com.ipartek.formacion.gestformwww.modelo.beans.Entrega;
import com.ipartek.formacion.gestformwww.modelo.beans.Material;

import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author  Jon
 */
public class VisualizarEnviarMailPedidoAccion implements Accion {

    private final String visualizarcursos = "visualizar-curso-accion";

    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        ModeloMaterial mmaterial = (ModeloMaterial) servlet.getServletContext().getAttribute("ModeloMaterial");
        ModeloEntrega mentrega = (ModeloEntrega) servlet.getServletContext().getAttribute("ModeloEntrega");
        AccionServlet as = (AccionServlet) servlet;
        HttpSession sesion = request.getSession();
        ResourceBundle bundle = (ResourceBundle) servlet.getServletContext().getAttribute("mailPlantillas-mapeo");
        AccionRouter router = null;
        ArrayList<Material> materiales = new ArrayList<Material>();
        Entrega entrega = new Entrega();
        Material material = new Material();

        ArrayList<String> errores = new ArrayList<String>();
        String[] id;
        if (sesion.getAttribute("usuario") != null && request.getMethod().equalsIgnoreCase("POST")) {
            if (request.getParameter("idCurso") != null) {
                if (request.getParameterValues("listaIdEntregaPedir") != null) {
                    id = request.getParameterValues("listaIdEntregaPedir");
                    for (String d : id) {
                        try {
                            entrega = mentrega.getEntrega(Integer.parseInt(d));
                            if (entrega != null) {
                                material = mmaterial.getMaterial(entrega.getLibId());
                                material.setCantidadPedido(entrega.getCantidad());
                            }
                            materiales.add(material);
                        } catch (GestorFormacionExcepcion ex) {
                            Logger.getLogger(VisualizarEnviarMailCandidatoAccion.class.getName()).log(Level.SEVERE, null, ex);
                            sesion.setAttribute("excepciones", ex.getMessage());
                        }
                    }

                    request.setAttribute("listamaterial", materiales);
                    request.setAttribute("contenido", "visualizar-enviar-mail-pedido");
                    router = new AccionRouter("principal");
                } else {
                    errores.add("error-seleccionar-material");
                    request.setAttribute("errores", errores);
                    request.setAttribute("idPestana", "4");
                    try {
                        router = as.getFactory().getAction(as.getNombreClaseRecurso(visualizarcursos), servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(VisualizarEnviarMailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(VisualizarEnviarMailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(VisualizarEnviarMailAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
        } else {
            router = new AccionRouter("inicio");
        }
        return router;

    }
}
